﻿using MySchool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace dierci
{
    public partial class Form3 : Form
    {
        DBHelper db = new DBHelper();
        DataSet set = new DataSet();
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            db.OpenConnection();
            string sql = "select * from StationID";
            SqlDataAdapter adapter = new SqlDataAdapter(sql,db.Connection);
            adapter.Fill(set, "StationID");
            this.comboBox1.DataSource = set.Tables["StationID"];
            this.comboBox1.ValueMember = "StationID";
            this.comboBox1.DisplayMember = "StationName";
            db.CloseConnection();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            int i=0;
            db.OpenConnection();
            string csj = "select StationID from StationID where StationName=N'" + this.comboBox1.Text+"'";
            SqlCommand comm = new SqlCommand(csj,db.Connection);
            SqlDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
                i =Convert.ToInt32(reader["StationID"]);
            }
            reader.Close();
            if (i==0)
            {
                MessageBox.Show("未找到该监测站");
                return;
            }

            if (this.textBox1.Text.Equals(string.Empty)|| this.textBox2.Text.Equals(string.Empty)||
                this.textBox3.Text.Equals(string.Empty) || this.textBox4.Text.Equals(string.Empty)||
                this.textBox5.Text.Equals(string.Empty))
            {
                MessageBox.Show("输入有误");
                return;
            }

           object t= Convert.ToDateTime(this.textBox2.Text);
            object t2 = Convert.ToDateTime(this.textBox3.Text);

            string sql = "insert AirQualityInfoo(StationID,InputName,StartDate,EndDate,PM,Level)values('"+i+"', N'"+this.textBox1.Text+"', '"+t + "', '" + t2 + "', N'"+ this.textBox4.Text + "', N'" + this.textBox5.Text + "')";
            SqlCommand command = new SqlCommand(sql,db.Connection);
            int it =Convert.ToInt32(command.ExecuteNonQuery()) ;
            if (it>0)
            {
                MessageBox.Show("成功");
                return;
            }
            db.CloseConnection();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = null;
            this.textBox2.Text = null;
            this.textBox3.Text = null;
            this.textBox4.Text = null;
            this.textBox5.Text = null;
        }
    }
}
